<template>
  <div class="header_ref">
    <div class="tabs_box">
      <ul>
        <li :class="{ 'active': isPublic === item.val }"
          v-for="item in [{ val: 1, text: '全部资料' }, { val: 0, text: '我的资料'}]"
          :key="item.val"
          @click="isPublic = item.val; publicChange(item.val)"
        >{{ item.text }}</li>
      </ul>
    </div>
    <div class="search">
      <el-input clearable placeholder="按文件名搜索" prefix-icon="el-icon-search" v-model="searchText" @keydown.enter="searchHandle" @clear="searchHandle" />
    </div>
    <div class="btns" v-permissions="'add'">
      <el-dropdown @command="handle">
        <el-button round><span>添加资料</span><i class="el-icon-caret-bottom" /></el-button>
        <template #dropdown>
          <el-dropdown-menu>
            <el-popover placement="left-start" trigger="hover" :width="350">
              <template #reference>
                <el-dropdown-item :command="null">上传资料</el-dropdown-item>
              </template>
              <p class="upload-format"><span class="format-title">【备课、授课】</span>课件支持的格式：.ppt .pptx</p>
              <p class="upload-format"><span class="format-title">【备课】</span>讲义支持的格式：.doc .docx .pdf</p>
              <p class="upload-format"><span class="format-title">【备课】</span>说课视频支持的格式：.mp4（H264）</p>
              <p class="upload-format"><span class="format-title">【备课、授课】</span>多媒体支持的格式：.mp3 .mp4</p>
              <p class="upload-format"><span class="format-title">【备课、授课】</span>其他支持的格式：.png .jpg .jpeg .mp3 .zip .rar</p>
            </el-popover>
            <el-popover placement="left-start" trigger="hover" :width="350">
              <template #reference>
                <el-dropdown-item :command="5">上传标准教案</el-dropdown-item>
              </template>
              <p class="upload-format"><span class="format-title">【备课】</span>标准教案支持的格式：.doc .docx</p>
            </el-popover>
            <el-dropdown-item v-permissions="'create'" :command="2" v-if="systemName !== 'xinbo'">新建讲义</el-dropdown-item>
            <el-popover placement="left-start" trigger="hover" :width="350">
              <template #reference>
                <el-dropdown-item :command="6">上传多媒体</el-dropdown-item>
              </template>
              <p class="upload-format"><span class="format-title">【备课】</span>多媒体支持的格式：.mp3 .mp4</p>
            </el-popover>
            <!-- <el-dropdown-item :command="1">新建H5课件</el-dropdown-item> -->
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>
<script lang="ts">
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import UploadComponent from './upload.vue';
import AddHandoutComponent from './add-handout.vue';
import $, { emitter } from '$'
import { useStore } from 'vuex';

export default {
  props: ['getKnowledge'],
  setup(props, { emit }) {

    let store = useStore();

    let searchText = ref(null);

    let isPublic = ref(1);

    const searchHandle = () => emit('search', { type: 'fileName', value: searchText.value});
    const publicChange = () => emit('search', { type: 'isPublic', value: isPublic.value })

    const handle = (type) => {
      props.getKnowledge().then((knowledgeList: any[]) => {
        if (type === 2) {
          $.modal.create({
            title: '新建讲义',
            width: 480,
            component: AddHandoutComponent,
            props: { knowledgeList, type }
          }).then((res) => {
            if (res.result) {
              ElMessage.success('新建讲义成功~！');
              emitter.emit('dataset-reset');
            }
          });
        } else {
          openUpload(type,knowledgeList)
        }
      })
    }
    const openUpload = async (type, knowledgeList) => {
      const hasPublicRole = store.getters.userInfo.roles.reduce((has, role) => role.isSystem || has || (role.menuUrls && role.menuUrls.includes('/teaching/database#public')), false);
      await $.modal.create({ 
        title: type === 5 ? '上传标准教案' : type === 6 ? '上传多媒体' : type === 1 ? '上传H5课件' : '上传资料', 
        width: 480, 
        component: UploadComponent, 
        props: { files: null, knowledgeList, type, isRadio: hasPublicRole } 
      });
      ElMessage.success('上传资料成功~！');
      emitter.emit('dataset-reset');
    }

    const systemName = import.meta.env.VITE_SYSTEM_NAME;

    return { searchText, searchHandle, handle, isPublic, publicChange, systemName }
  }
}
</script>
<style lang="scss">
  .upload-format{
    // line-height: 25px;
    .format-title {
      color: $--color-remind;
    }
  }
</style>
<style lang="scss" scoped>
.header_ref {
  display: flex;
  line-height: 60px;
  .tabs_box {
    li {
      float: left;
      padding: 0 20px;
      color: #fff;
      list-style: none;
      position: relative;
      cursor: pointer;
      &.active::after {
        content: '';
        display: block;
        width: 100%;
        height: 6px;
        background: #FAAD14;
        border-radius: 3px;
        position: absolute;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
      }
    }
  }
  .search {
    margin-left: auto;
    :deep(.el-input__prefix),
    :deep(.el-input__suffix) {
      color: #fff !important;
    }
    :deep(input) {
      width: 240px;
      height: 36px;
      color: #fff;
      border: 0;
      border-radius: 18px;
      background: rgba(255, 255, 255, 0.3);
      &::placeholder {
        color: rgba(255, 255, 255, .7);
      }
    }
  }
  .btns {
    margin-left: 30px;
    button {
      color: #72b22a;
      padding: 10px 23px;
      input {
        display: none;
      }
    }
    p{
      line-height: 25px;
    }
  }
}
</style>
